package com.vdimitr.happycube.puzzle.solution.output.conversion;

import com.vdimitr.happycube.puzzle.piece.PuzzlePiece;

/**
 * This class is responsible for concatenating a matrix of puzzle pieces to a
 * matrix of strings where each string represents the concatenated strings of
 * the matrix representations of the puzzle pieces in a row
 * 
 * @see PuzzlePieceMatrixRowToStringConverter
 * 
 * @author vasileios.dimitriadis
 * 
 */
public class PuzzlePiecesToStringConcantenator {

	/**
	 * Used for converting matrix rows to strings
	 */
	private PuzzlePieceMatrixRowToStringConverter matrixRowToStringConverter = null;

	/**
	 * 
	 * @param matrixRowToStringConverter
	 *            the converter to use for creating string representations
	 */
	public PuzzlePiecesToStringConcantenator(
			PuzzlePieceMatrixRowToStringConverter matrixRowToStringConverter) {
		this.matrixRowToStringConverter = matrixRowToStringConverter;
	}

	/**
	 * Converts rows of puzzle pieces to rows of strings
	 * 
	 * @param puzzlePieces
	 *            a matrix of puzzle pieces to be converted to a concatenated
	 *            matrix of strings
	 * @return a matrix of strings where each string represents the concatenated
	 *         strings of the matrix representations of the puzzle pieces in a
	 *         row
	 */
	public String[] concantenatePuzzlePiecesToString(PuzzlePiece[] puzzlePieces) {
		String[] puzzlePieceArrayToString = null;
		for (PuzzlePiece puzzlePiece : puzzlePieces) {
			String[] puzzlePieceMatrixToString = matrixRowToStringConverter
					.toString(puzzlePiece);
			if (puzzlePieceArrayToString == null) {
				puzzlePieceArrayToString = puzzlePieceMatrixToString;
			} else {
				for (int i = 0; i < puzzlePieceMatrixToString.length; i++) {
					puzzlePieceArrayToString[i] += puzzlePieceMatrixToString[i];
				}
			}
		}
		return puzzlePieceArrayToString;
	}

}
